package com.cqupt.command.imp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cqupt.command.Command;
import com.cqupt.utils.FileReadMan;
import com.cqupt.utils.Result;

public class SortCommand implements Command {
	// 选项列表
	private List<String> options;
	// 参数列表..
	private String parameters;
	// 上命令的结果
	private String result;

	public SortCommand(String command, String result) {
		// 命令解析得到选项
		this.options = getOptions(command);
		// 命令解析得到参数
		this.parameters = getParameters(command);
		// 得到上个命令的结果
		this.result = result;
	}

	private List<String> getOptions(String command) {
		List<String> result = new ArrayList<>();
		Pattern pattern = Pattern.compile("-[a-z,A-Z]+");
		Matcher matcher = pattern.matcher(command);
		while (matcher.find()) {
			String parameterA = matcher.group();
			char[] chars = parameterA.substring(1, parameterA.length()).toCharArray();
			for (int i = 0; i < chars.length; i++) {
				result.add(chars[i] + "");
			}
		}
		return result;
	}

	private String getParameters(String command) {
		String[] cmd = command.split(" ");
		return cmd[cmd.length - 1].trim();
	}

	// -u去重复，-n以数字排序，-r降序排序，默认是升序排序，以字符排序
	@Override
	public String run() {
		// 确定选项
		boolean distinct = false;// 是否去重
		boolean number = false;// 是否以数字排序
		boolean desc = false;// false：默认升序排序，true:降序排序
		for (int i = 0; i < options.size(); i++) {
			String option = options.get(i);
			switch (option) {
			case "u":
				distinct = true;
				break;
			case "n":
				number = true;
				break;
			case "r":
				desc = true;
				break;
			}
		}
		// 获取数据源
		List<String> listString = new ArrayList<>();
		List<Double> listDouble = new ArrayList<>();
		if (result != null) {
			String[] resultSource = result.split("\r?\n");
			for (int i = 0; i < resultSource.length; i++) {
				String sourceLine = resultSource[i];
				if (!"".equals(sourceLine)) {
					listString.add(sourceLine.trim());
				}
				if (number && sourceLine.matches("-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|\\d+)")) {
					Double sourceLintDouble = Double.parseDouble(sourceLine);
					listDouble.add(sourceLintDouble);
				}
			}
		} else {
			FileReadMan fileReadMan = new FileReadMan(parameters);
			while (fileReadMan.hasNext()) {
				String sourceLine = fileReadMan.next();
				if (!"".equals(sourceLine)) {
					listString.add(sourceLine.trim());
				}
				if (number && sourceLine.matches("-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|\\d+)")) {
					Double sourceLintDouble = Double.parseDouble(sourceLine);
					listDouble.add(sourceLintDouble);
				}
			}
		}
		// 排序
		if (number) {
			if (distinct) {
				Set<Double> set = new HashSet<Double>(listDouble);
				listDouble = new ArrayList<Double>(set);
			}
			Collections.sort(listDouble);
			if (desc) {
				Collections.reverse(listDouble);
			}
			Result<Double> result = new Result<Double>();
			return result.getResult(listDouble);
		} else {
			if (distinct) {
				Set<String> set = new HashSet<String>(listString);
				listString = new ArrayList<String>(set);
			}
			Collections.sort(listString);
			if (desc) {
				Collections.reverse(listString);
			}
			Result<String> result = new Result<String>();
			return result.getResult(listString);
		}
	}
}